import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";

import { SkipJwtAuth } from "@/core/auth/decorator/skip-auth.decorator";
import { ApiBaseResponse } from "@/core/decorators/api-base-response.decorator";
import { ApiPageResponse } from "@/core/decorators/api-page-response.decorator";
import AjaxResult from "@/core/utils/AjaxResult";

import { CategoryService } from "./category.service";
import { CreateCategoryDto } from "./dto/create-category.dto";
import { QueryCategoryDto } from "./dto/query-category.dto";
import { UpdateCategoryDto } from "./dto/update-category.dto";
import { CategoryEntity } from "./entities/category.entity";

@SkipJwtAuth()
@ApiTags("分类")
@Controller("category")
export class CategoryController {
    constructor(private readonly categoryService: CategoryService) {}

    @ApiOperation({ summary: "分类添加" })
    @ApiBaseResponse(CategoryEntity)
    @Post("/add")
    async create(@Body() createCategoryDto: CreateCategoryDto) {
        return AjaxResult.success(await this.categoryService.create(createCategoryDto));
    }

    @ApiOperation({ summary: "分类列表" })
    @ApiBaseResponse({ type: "array", items: CategoryEntity })
    @Get("/list")
    async list() {
        return AjaxResult.success(await this.categoryService.findAll());
    }
    @ApiOperation({ summary: "分类分页" })
    @ApiPageResponse(CategoryEntity)
    @Get("/page")
    async page(@Query() query: QueryCategoryDto) {
        return AjaxResult.success(await this.categoryService.findPage(query));
    }

    @ApiOperation({ summary: "分类详情" })
    @ApiBaseResponse(CategoryEntity)
    @Get("detail/:id")
    async findOne(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.categoryService.findOne(+id));
    }

    @ApiOperation({ summary: "分类修改" })
    @ApiBaseResponse(CategoryEntity)
    @Patch("/edit/:id")
    async update(@Param("id", ParseIntPipe) id: number, @Body() updateCategoryDto: UpdateCategoryDto) {
        return AjaxResult.success(await this.categoryService.update(+id, updateCategoryDto));
    }

    @ApiOperation({ summary: "分类删除" })
    @Delete("/delete/:id")
    async remove(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.categoryService.remove(+id));
    }
}
